import sessionStorageUtil from '@/utils/sessionStorageUtil'
import localStorageUtil from '@/utils/localStorageUtil'


// 获取当前用户的所属组织
export function getOrganId() {
  return sessionStorageUtil.fetch('LOGIN_USER_ORGAN')
}

// 获取当前用户的所属组织
export function getTenantId() {
  return sessionStorageUtil.fetch('LOGIN_USER_TENANT')
}


// 获取当前用户的所属组织路径
export function getOrganPath() {
  return sessionStorageUtil.fetch('LOGIN_USER_ORGAN_PATH')
}

// 过滤出系统错误

// 过滤出系统错误
export function filterSystemErrorCode(dictionaryList) {

  const errorCodeFilter = "ERROR_CODE"
  const errorCodeItem = []
  if (dictionaryList || dictionaryList.length) {
    dictionaryList.forEach(function(element) {
      if (element.nameSpace === errorCodeFilter) {
        errorCodeItem.push(element)
      }
    })
    const errorCodeMap = {}
    errorCodeItem.forEach(function(element) {
      if (element.nameSpace === errorCodeFilter) {
        errorCodeMap[element.value] = element.desc
      }
    })
    localStorageUtil.save('error_map', errorCodeMap)
  }
}


// 过滤出系统错误
export function getSystemErrorCode(code) {
  let errorMap = localStorageUtil.fetch('error_map')
  if (errorMap[code]) {
    return errorMap[code]
  }
  return '系统错误'
}


// 获取当前用户ID
export function getCurrentLoginUserId() {
  return sessionStorageUtil.fetch('USER_ID')
}


export function getSelectedNodes(dataList, property) {
  const selectedNodes = []
  const selectedKeys = {}
  for (let i = 0; i < dataList.length; i++) {
    if (dataList[i][property]) {
      selectedNodes.push(dataList[i].id)
      selectedKeys[dataList[i].id] = true
    }
  }
  return selectedNodes
}

export function getSelectedNodesObject(dataList, property) {
  debugger
  const selectedNodes = []
  for (let i = 0; i < dataList.length; i++) {
    if (dataList[i][property]) {
      selectedNodes.push(dataList[i])
    }
  }
  return selectedNodes
}


// 获取超级管理员ID
export function getSuperManagerId() {
  return 1
}

// 获取超级管理员ID
export function getPlatformOrgan() {
  return 1
}


// 构建数据结构数据
export function handleBuildTreeDataStruct(dataList, idKey, pidKey) {
  const treeDataStruct = []
  const ids = {}
  const pids = {}
  for (let i = 0; i < dataList.length; i++) {
    // 转换数织为map
    ids[dataList[i][idKey]] = dataList[i]

    if (pids[dataList[i][pidKey]] && pids[dataList[i][pidKey]].length) {
      pids[dataList[i][pidKey]].push(dataList[i])
    } else {
      pids[dataList[i][pidKey]] = []

      pids[dataList[i][pidKey]].push(dataList[i])
      // ids[dataList[i][idKey]]['isDelete'] = true;
    }
  }

  for (const key in pids) {
    if (ids[key]) {
      ids[key]['children'] = pids[key]
    } else {
      for (let j = 0; j < pids[key].length; j++) {
        treeDataStruct.push(pids[key][j])
      }
    }
  }
  return treeDataStruct
}

// 获取当前日期时间
export function getCurrentTimeYYYY_MM_dd_HH_mm_ss() {
  var myDate = new Date;
  var year = myDate.getFullYear(); //获取当前年
  var mon = myDate.getMonth() + 1 < 10 ? "0" + (myDate.getMonth() + 1) : myDate.getMonth() + 1; //获取当前月
  var date = myDate.getDate() < 10 ? "0" + myDate.getDate() : myDate.getDate(); //获取当前日
  var hour = myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours(); //获取当前时
  var minute = myDate.getMinutes() < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes(); //获取当前分
  return year + "-" + mon + "-" + date + " " + hour + ":" + minute + ":00";
}


// 获取当前日期时间
export function upload() {
  var myDate = new Date;
  var year = myDate.getFullYear(); //获取当前年
  var mon = myDate.getMonth() + 1 < 10 ? "0" + (myDate.getMonth() + 1) : myDate.getMonth() + 1; //获取当前月
  var date = myDate.getDate() < 10 ? "0" + myDate.getDate() : myDate.getDate(); //获取当前日
  var hour = myDate.getHours() < 10 ? "0" + myDate.getHours() : myDate.getHours(); //获取当前时
  var minute = myDate.getMinutes() < 10 ? "0" + myDate.getMinutes() : myDate.getMinutes(); //获取当前分
  return year + "-" + mon + "-" + date + " " + hour + ":" + minute + ":00";
}
